<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>插入新数据并重新从小到大排序</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            display: flex;
            width: 400px;
            margin: 50px auto 0;
        }
        p{
            width: 400px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div>
        <input type="text" id='newNum'>
        <button id='btn'>插入数字</button>
    </div>
    <p>dsd</p>

    <script>
        var p = document.getElementsByTagName('p')[0];

        // 初始化
        var arr = [10,43,2,54,203,134];
        p.innerHTML = '[' + arr.sort(function(a,b){return a-b}) + ']';

        // 点击插入新数据
        btn.onclick = function(){
            var newNumber = newNum.value*1;
            // 添加到数组
            arr.push(newNumber);
            // 排序后写入p标签
            p.innerHTML = '[' + arr.sort(function(a,b){return a-b}) + ']';
            // 清空输入框
            newNum.value = '';
        }


    </script>
</body>
</html>